# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2022, Intel Corporation

cmake_minimum_required(VERSION 3.5)

project(PCM)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# set(CMAKE_POSITION_INDEPENDENT_CODE ON)


option(PCM_NO_ASAN "Disable address sanitizer" OFF)
option(PCM_FUZZ "Enable fuzzing" OFF)
option(PCM_BUILD_EXECUTABLES "Build PCM utilities" ON)
option(PCM_NO_STATIC_LIBASAN OFF)

if(MSVC)
    option(PCM_NO_STATIC_MSVC_RUNTIME_LIBRARY "Disable using static runtime under MSVC" OFF)
endif()

foreach(opt NO_STATIC_MSVC_RUNTIME_LIBRARY;FUZZ;NO_ASAN;NO_STATIC_LIBASAN)
    if(${opt})
        message(DEPRECATION "Option \"${opt}\" is deprecated and will be removed soon. Please use \"PCM_${opt}\"")
        set(PCM_${opt} ${opt})
    endif()
endforeach()

include(GNUInstallDirs)

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

# Check for pcm*.x artifacts generated by an old build scenario using Makefiles
file(GLOB_RECURSE PCM_X_ARTIFACTS ${CMAKE_CURRENT_SOURCE_DIR}/*.x)
foreach(file ${PCM_X_ARTIFACTS})
    file(REMOVE ${file})
    message(STATUS "Removing old artifact from current source directory : ${file}")
endforeach()

if(PCM_X_ARTIFACTS)
    message(WARNING
	  " Old pcm utilities (.x) were indicated in build folder.\n"
          " Old binaries are expected to be installed in system.\n"
          " Make sure to install the new binaries(run 'cmake --install .') after building.)")
endif()

message(STATUS "System: ${CMAKE_SYSTEM}")
if(UNIX AND NOT APPLE)
    if(${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
        set(FREE_BSD TRUE)
    else()
        set(LINUX TRUE)
    endif()
endif()

if(UNIX)  # APPLE, LINUX, FREE_BSD
    if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
      set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type (default Release)" FORCE)
    endif()
    message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")

    message(STATUS "initial CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
    message(STATUS "initial CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}")

    # required PCM common flags
    set (PCM_COMMON_FLAGS "-Wno-unknown-pragmas -fPIC -DCMAKE_INSTALL_PREFIX=\"${CMAKE_INSTALL_PREFIX}\"")

    if(LINUX)
       set (PCM_COMMON_FLAGS "${PCM_COMMON_FLAGS} -Wextra -DPCM_USE_PERF")
       if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
         set (PCM_COMMON_FLAGS "${PCM_COMMON_FLAGS} -Wl,-z,now")
       endif()

       if(NOT DEFINED LINUX_SYSTEMD)
         set(LINUX_SYSTEMD FALSE)
       endif()

       if(NOT DEFINED LINUX_SYSTEMD_UNITDIR)
           set(LINUX_SYSTEMD_UNITDIR /usr/lib/systemd/system)
       endif()

       if(LINUX_SYSTEMD)
         message(STATUS "A systemd unit file will be generated")
         message(STATUS "LINUX_SYSTEMD_UNITDIR:${LINUX_SYSTEMD_UNITDIR}")
       else()
         message(STATUS "Set LINUX_SYSTEMD=TRUE for a systemd unit file.")
       endif()
    endif(LINUX)

    # adding the required PCM common flags
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${PCM_COMMON_FLAGS}")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${PCM_COMMON_FLAGS}")

    message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
    message(STATUS "CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}")

    set(PCM_OPTIONAL_FLAGS "-Wall")
    if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
         set (PCM_DYNAMIC "-rdynamic")
    elseif()
         set (PCM_DYNAMIC "")
    endif()
    if(PCM_NO_ASAN)
        message(STATUS "AddressSanitizer is disabled")
        set(PCM_ASAN "")
    else()
        message(STATUS "AddressSanitizer is enabled")
        message(STATUS "To disable AddressSanitizer, use -DPCM_NO_ASAN=1 option")
        set(PCM_ASAN "-fsanitize=address")
    endif()
    set(PCM_HARDENING_FLAGS "-fPIE -fstack-protector -D_FORTIFY_SOURCE=2 -ftrapv ${PCM_ASAN} -fwrapv -fno-delete-null-pointer-checks -fno-strict-overflow -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer")
    if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5)
        message(WARNING "Old gcc compiler (version < 5), -fsanitize=undefined option is not supported.")
    elseif()
        set(PCM_HARDENING_FLAGS "${PCM_HARDENING_FLAGS} -fsanitize=undefined")
    endif()
    set(PCM_LINKER_HARDENING_FLAGS "${PCM_ASAN}")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PCM_LINKER_HARDENING_FLAGS}")
    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PCM_LINKER_HARDENING_FLAGS}")
    set(CMAKE_CXX_FLAGS_RELEASE "${PCM_OPTIONAL_FLAGS} -O3 ${PCM_DYNAMIC} ${PCM_HARDENING_FLAGS}")
    set(CMAKE_CXX_FLAGS_DEBUG "${PCM_OPTIONAL_FLAGS} -O0 -g ${PCM_DYNAMIC} ${PCM_HARDENING_FLAGS}")
    set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${PCM_OPTIONAL_FLAGS} -O3 -g ${PCM_DYNAMIC} ${PCM_HARDENING_FLAGS}")
    if(FREE_BSD)
        set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -lexecinfo")
        set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -lexecinfo")
        set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -lexecinfo")
    endif(FREE_BSD)
    set(CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
    set(CMAKE_C_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
    set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")

    message(STATUS "CMAKE_CXX_FLAGS_RELEASE: ${CMAKE_CXX_FLAGS_RELEASE}")
    message(STATUS "CMAKE_CXX_FLAGS_DEBUG: ${CMAKE_CXX_FLAGS_DEBUG}")
    message(STATUS "CMAKE_CXX_FLAGS_RELWITHDEBINFO: ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")

endif(UNIX)

if(PCM_FUZZ)
    set(FUZZER_OPTIONS "-fsanitize=fuzzer,address -fprofile-instr-generate -fcoverage-mapping")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FUZZER_OPTIONS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FUZZER_OPTIONS}")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FUZZER_OPTIONS}")
endif(PCM_FUZZ)

#######################
# Install
#######################

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

add_subdirectory(src)
add_subdirectory(examples)
add_subdirectory(tests)

message(STATUS "Install directory: ${CMAKE_INSTALL_PREFIX}")

#######################
# CPack (only UNIX)
#######################
if(UNIX)
    if(EXISTS "/etc/debian_version")      # for Debian family
        set(CPACK_GENERATOR "DEB")
        message(STATUS "CPACK_GENERATOR is DEB")
    elseif(EXISTS "/etc/redhat-release")  # for RHEL, Fedora, CentOs
        set(CPACK_GENERATOR "RPM")
        message(STATUS "CPACK_GENERATOR is RPM")
    else()
        if(EXISTS "/etc/os-release")
            file(READ "/etc/os-release" OS_PARAMS)
            string(REGEX MATCH "suse" OUT ${OS_PARAMS})  # for Suse-like systems
            if(OUT STREQUAL "suse")
                 set(CPACK_GENERATOR "RPM")
                 message(STATUS "CPACK_GENERATOR is RPM")
            else()
                set(CPACK_GENERATOR "TXZ")
                set(CPACK_SET_DESTDIR ON)
                message(STATUS "CPACK_GENERATOR is TXZ")
            endif()
        else()
            set(CPACK_GENERATOR "TXZ")
            set(CPACK_SET_DESTDIR ON)
            message(STATUS "CPACK_GENERATOR is TXZ")
        endif()
    endif()

    set(CPACK_PACKAGE_CONTACT "intel <roman.dementiev@intel.com>")
    set(CPACK_PACKAGE_NAME    "pcm")
    set(CPACK_PACKAGE_VERSION "0000")

    set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Intel(r) Performance Counter Monitor (Intel(r) PCM)")
    set(CPACK_PACKAGE_DESCRIPTION "\
    Intel(r) Performance Counter Monitor (Intel(r) PCM) is an application programming\n\
    interface (API) and a set of tools based on the API to monitor\n\
    performance and energy metrics of Intel(r) Core(tm), Xeon(r), Atom(tm)\n\
    and Xeon Phi(tm) processors. PCM works on Linux, Windows, Mac OS X,\n\
    FreeBSD and DragonFlyBSD operating systems.")
    set(CPACK_RPM_PACKAGE_DESCRIPTION ${CPACK_PACKAGE_DESCRIPTION})

    set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
    set(CPACK_RPM_PACKAGE_LICENSE   "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")

    set(CPACK_PACKAGE_INSTALL_DIRECTORY             ${CMAKE_INSTALL_PREFIX})
    set(CPACK_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)

    set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
    set(CPACK_RPM_PACKAGE_RELOCATABLE  TRUE)

    set(CPACK_DEB_COMPONENT_INSTALL ON)
    set(CPACK_RPM_COMPONENT_INSTALL ON)

    include (CPack)
endif(UNIX)
